﻿import QtQuick 2.0
import Box2D 2.0
import "./tree"
import "./shared"
import QtQuick.Controls 1.4
import QtMultimedia 5.5



Item {
    id: screen
    width: 350
    height: 1040

    property var birds: new Array
    property var currentItem: undefined
    property real power: 1
    property var myMonster: undefined
    property int score: 0

    function restartGame(){
        if(myMonster) myMonster.destroy()

        for(var i in birds){
            birds[i].destroy()
        }
        birds.length = 0

        guanka.create()

        myMonster = monster.createObject(screen)
        imgBoom.x = myMonster.x + myMonster.width/2 - imgBoom.width/2
        imgBoom.y = myMonster.y + myMonster.height/2 - imgBoom.height/2
    }

    Component.onCompleted: {
        timer.restart()
    }

    Timer{
        id: timer
        interval: 1000
        onTriggered: {
            restartGame()
        }
    }

    Image{
        id: imgBg
        anchors.fill: parent
        source: "qrc:/imamges/image/gamebg.png"
    }


//    Button{
//        text: "debug"
//        z: 1
//        onClicked: {

//            debug.visible = !debug.visible

//            console.log("------",physicsWorld.timeStep)
//        }
//    }
//    Button{
//        text: "game"
//        y: 30
//        z: 1
//        onClicked: {
//            restartGame()
//        }
//    }

    Component {
        id: rectComponent

        PhysicsItem {
            id: ball
            width: 40
            height: 40
            objectName: "bird"

            Image{
                anchors.fill: parent
                fillMode: Image.Stretch
                source: "qrc:/imamges/image/bird.png"
            }

            property Body body: CircleBody {
                gravityScale: 0
                target: ball
                world: physicsWorld
                sleepingAllowed: false
                bodyType: Body.Dynamic

                radius: ball.width/2
                density: 2
                friction: 1
                restitution: 0.05
                linearDamping: 0.5
                angularDamping: 0

//                onBeginContact: {
//                    var name = other.getBody().target.objectName
//                    console.log(name)
//                    if(name == "monster" || name == "wood" || name == "stone"){
//                        soundCollision.play()
//                    }
//                }
            }
        }

    }
    World {
        id: physicsWorld
        timeStep: 1/60 * 2
    }


    Ground{
        id: ground
        z: 2
    }

    Image{
        id: bar
        x: 300
        anchors.bottom: ground.top
        source: "qrc:/imamges/image/bar.png"
    }

    //绳子
    Canvas{
        id: canvasBottom
        anchors.fill: parent
        onPaint: {
            var context = canvasBottom.getContext("2d");
            context.clearRect(0,0,canvasBottom.width,canvasBottom.height)

            if(currentItem != undefined){
                var angle = Math.atan2((currentItem.y-663),(currentItem.x-301))*180/Math.PI
//                console.log(angle)

                context.beginPath();
                context.moveTo(336,708-45);
                if(angle > 170){
                    context.lineTo(currentItem.x,currentItem.y+currentItem.height/2);
                }
                else if(angle < -170){
                    //左上
                    context.lineTo(currentItem.x,currentItem.y+currentItem.height/2);
                }
                else if(angle < 0){
                    //up
                    context.lineTo(currentItem.x,currentItem.y);
                }
                else{
                    //中下
                    context.lineTo(currentItem.x,currentItem.y+currentItem.height);
                }

                context.strokeStyle = "#371d10";
                context.lineWidth = 8;
                context.lineCap = "round"
                context.stroke();
            }
            else{
                context.beginPath();
                context.moveTo(336,708-45);
                context.lineTo(301-20,663+20);
                context.strokeStyle = "#371d10";
                context.lineWidth = 8;
                context.lineCap = "round"
                context.stroke();
            }
        }
    }
    Canvas{
        id: canvas
        anchors.fill: parent
        z: 1
        onPaint: {
            var context = canvas.getContext("2d");
            context.clearRect(0,0,canvas.width,canvas.height)

            if(currentItem != undefined){
                 var angle = Math.atan2((currentItem.y-663),(currentItem.x-301))*180/Math.PI

                context.beginPath();
                context.moveTo(305,704-45);
                if(angle > 170){
                    context.lineTo(currentItem.x,currentItem.y+currentItem.height/2);
                }
                else if(angle < -170){
                    //左上
                    context.lineTo(currentItem.x,currentItem.y+currentItem.height/2);
                }
                else if(angle < 0){
                    //up
                    context.lineTo(currentItem.x,currentItem.y);
                }
                else{
                    //中下
                    context.lineTo(currentItem.x,currentItem.y+currentItem.height);
                }
                context.strokeStyle = "#371d10";
                context.lineWidth = 8;
                context.lineCap = "round"
                context.stroke();
            }
            else{
                context.beginPath();
                context.moveTo(305,704-45);
                context.lineTo(301-20,663+20);
                context.strokeStyle = "#371d10";
                context.lineWidth = 8;
                context.lineCap = "round"
                context.stroke();
            }
        }
    }

    Image{
        id: bar2
        x: 300
        z: 1
        anchors.bottom: ground.top
        source: "qrc:/imamges/image/bar2.png"
    }

    Image{
        id: barCao
        x: 289
        y: 738-55 + 11
        z: 1
        source: "qrc:/imamges/image/cao.png"
    }

    //guanka
    GuanKa{
        id: guanka
    }

    //pig
    Component{
        id: monster
        ImgBody{
            id: pig
            objectName: "monster"
            x: parent.width - width - 200
            y: parent.height - ground.height - height
            source: "qrc:/imamges/image/pig.png"
            body.bodyType: Body.Dynamic

            onBeginContact: {
                var name = other.getBody().target.objectName
//                console.log("===",name)
                if(name == "bird" || name == "wood" || name == "stone"){
                    myMonster.destroy()
                    imgBoom.opacity = 1
                    boomAnim.restart()
                    score++
                    soundCollision.play()
                }
            }

            fixtures: [
                Polygon {
                    friction: 1
                    density: 1
                    restitution: 0.2
                    vertices: [
                        Qt.point(10.89,38.5),
                        Qt.point(1.96,73.86),
                        Qt.point(16.25,92.07),
                        Qt.point(38.39,98.5),
                        Qt.point(78.04,92.79),
                        Qt.point(92.68,72.43),
                        Qt.point(79.82,37.43),
                        Qt.point(44.46,24.57),
                    ]
                    onBeginContact: {
                        pig.beginContact(other)
                    }
                },
                Polygon {
                    friction: 1
                    density: 1
                    restitution: 0.2
                    vertices: [
                        Qt.point(13.31,38.03),
                        Qt.point(4.66,29.92),
                        Qt.point(35.74,8.3),
                        Qt.point(62.23,2.35),
                        Qt.point(85.2,14.78),
                        Qt.point(72.77,31.54),
                        Qt.point(37.91,25.05),
                    ]
                    onBeginContact: {
                        pig.beginContact(other)
                    }
                }
            ]
        }
    }

    Image{
        id: imgBoom
        source: "qrc:/imamges/image/boom.png"
        opacity: 0
        visible: opacity != 0
//        x: parent.width - pig.width - 200  + pig.width/2 - imgBoom.width/2
//        y: parent.height - ground.height + pig.height  - imgBoom.height/2
    }

    SequentialAnimation{
        id: boomAnim
        PropertyAnimation{target: imgBoom; property: "opacity"; from: 1; to: 0; duration: 1000}
        PauseAnimation {
            duration: 1000
        }
        onStopped: {
            restartGame()
        }
    }

    MouseArea{
        id: mouseFly
        anchors.fill: parent

        property real startX: 0
        property real startY: 0

        property real startXP: 0
        property real startYP: 0

        onPressed: {
            var obj = rectComponent.createObject(screen)
            obj.x = 301 - obj.width/2
            obj.y = 663 - obj.height/2
            birds.push(obj)

            startX = mouseX
            startY = mouseY

            startXP = obj.x
            startYP = obj.y

            currentItem = obj

            soundDan.play()
        }
        onPositionChanged: {
            if(currentItem==undefined) return

            if(currentItem.x + mouseX - startX > 281){
                currentItem.x = 281
            }
            else{
                currentItem.x += mouseX - startX
            }
//            currentItem.x += mouseX - startX
            currentItem.y += mouseY - startY

            if(currentItem.y > 715)  currentItem.y = 715
            if(currentItem.x < 0)  currentItem.x = 0

            startX = mouseX
            startY = mouseY

            currentItem.rotation = -Math.atan2(Math.abs(currentItem.y-startYP),Math.abs(currentItem.x-startXP)) * 180/Math.PI

            canvas.requestPaint()
            canvasBottom.requestPaint()

        }
        onReleased: {
            if(currentItem==undefined) return

            var dx = startXP - currentItem.x//currentItem.x + currentItem.width/2
            var dy = startYP - currentItem.y//currentItem.y + currentItem.height/2

            currentItem.body.applyLinearImpulse(Qt.point(dx*power,dy*power),currentItem.body.getWorldCenter())

            currentItem.body.angularVelocity = 20

            currentItem.body.gravityScale = 1

            currentItem = undefined

            canvas.requestPaint()
            canvasBottom.requestPaint()

            soundFly.play()
        }
    }

    //tips
    Text {
        id: tips
        text: "score: " + score
        anchors.horizontalCenter: parent.horizontalCenter
        y: 100
        font.pixelSize: 100
    }

    ScreenBoundaries{}

    DebugDraw {
        id: debug
        world: physicsWorld
        opacity: 0.75
        visible: false
    }

    Text {
        id: txtFps
        anchors.right: parent.right
        anchors.rightMargin: 5
        text: "fps:" + 1 / physicsWorld.timeStep
    }

    SoundEffect{
        id: soundDan
        source: "qrc:/qml/sound/愤怒小鸟-弹弓悬_爱给网_aigei_com.wav"
    }
    SoundEffect{
        id: soundCollision
        source: "qrc:/qml/sound/愤怒小鸟-tnt 盒爆炸_爱给网_aigei_com.wav"
    }
    SoundEffect{
        id: soundFly
        source: "qrc:/qml/sound/愤怒小鸟-鸟04 飞行_爱给网_aigei_com.wav"
    }
}
